package com.douqu.game.activity.entity.camp;


import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.activity.GameServer;
import com.douqu.game.activity.entity.MainServerInfo;
import com.douqu.game.activity.entity.e.E_PlayerStatus;
import com.douqu.game.core.e.E_MatchType;
import com.douqu.game.core.entity.EntityObject;
import com.douqu.game.core.entity.activity.MatchBaseInfo;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.util.LogUtils;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-03-08 20:59
 */
public class CampPlayer extends EntityObject{

    private MatchBaseInfo playerBase;

    private E_PlayerStatus playerStatus;

    private long createTime;

    private long updateTime;

    public CampPlayer()
    {
        this.playerStatus = E_PlayerStatus.WAIT_MATCH;
        this.createTime = this.updateTime = DataFactory.currentTime;
    }

    public CampPlayer(String objectIndex)
    {
        this();

        this.playerBase = new MatchBaseInfo(objectIndex);
    }

    public CampPlayer(MatchBaseInfo playerBase)
    {
        this();

        this.playerBase = playerBase;
    }

    public void loadFrom(ByteBuffer buffer)
    {
        playerBase = new MatchBaseInfo();
        playerBase.loadFrom(buffer);

        this.createTime = buffer.readLong();
    }

    public void writeTo(ByteBuffer buffer)
    {
        playerBase.writeTo(buffer);

        buffer.writeLong(createTime);
    }

    public boolean isCanMatch(CampPlayer target)
    {
        if(playerBase.getMatchList() == null)
            return true;

        return !playerBase.getMatchList().contains(target.getPlayerBase().getObjectIndex());
    }

    public void sendMatchSuccess(String teamIndex, E_MatchType matchType, MatchBaseInfo baseInfo)
    {
        if(playerBase.getServerId() == 0)
            return;

        MainServerInfo mainServerInfo = GameServer.getInstance().getMainServerInfo(playerBase.getServerId());
        if(mainServerInfo == null)
        {
            LogUtils.error("玩家的游戏主服为空 -> " + playerBase);
            return;
        }

        mainServerInfo.sendMatchSuccess(teamIndex, playerBase.getObjectIndex(), matchType, baseInfo);
    }


    public void updatePlayerBase(MatchBaseInfo baseInfo)
    {
        playerBase.update(baseInfo);
        updateTime = DataFactory.currentTime;
    }

    public MatchBaseInfo getPlayerBase() {
        return playerBase;
    }

    public void setPlayerBase(MatchBaseInfo playerBase) {
        this.playerBase = playerBase;
    }


    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public long getUpdateTime() {
        return updateTime;
    }


    public E_PlayerStatus getPlayerStatus() {
        return playerStatus;
    }

    public void setPlayerStatus(E_PlayerStatus playerStatus, long currentTime)
    {
        this.playerStatus = playerStatus;
        this.updateTime = currentTime;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CampPlayer)) return false;

        CampPlayer that = (CampPlayer) o;

        if (!playerBase.getObjectIndex().equals(that.getPlayerBase().getObjectIndex())) return false;

        return true;
    }

    @Override
    public int hashCode() {
        if(playerBase != null && playerBase.getObjectIndex() != null)
            return playerBase.getObjectIndex().hashCode();

        return 0;
    }

    @Override
    public String toString() {
        return "{" +
                "playerBase=" + playerBase +
                '}';
    }
}
